package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ServiceBorrowReturnDTO;
import com.eastfair.venueservice.dto.ServiceBorrowReturnPageQuery;
import com.eastfair.venueservice.dto.ServiceBorrowReturnSaveDTO;
import com.eastfair.venueservice.dto.ServiceBorrowReturnUpdateDTO;
import com.eastfair.venueservice.entity.ServiceBorrowReturn;
import com.eastfair.venueservice.service.ServiceBorrowReturnService;
import com.eastfair.venueservice.vo.ServiceBorrowReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 借用归还记录
 * </p>
 *
 * @author linan
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceBorrowReturn")
@Api(value = "ServiceBorrowReturn", tags = "借用归还记录")
@PreAuth(replace = "venueservice:serviceBorrowReturn:")
public class ServiceBorrowReturnController extends SuperController<ServiceBorrowReturnService, Long, ServiceBorrowReturn, ServiceBorrowReturnPageQuery, ServiceBorrowReturnSaveDTO, ServiceBorrowReturnUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ServiceBorrowReturnService serviceBorrowReturnService;

    @Override
    public void handlerResult(IPage<ServiceBorrowReturn> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceBorrowReturn> serviceBorrowReturnList = list
                .stream()
                .map((map) -> ServiceBorrowReturn.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(serviceBorrowReturnList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceBorrowReturnPageQuery> params) {
        ServiceBorrowReturnPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询借还记录VO列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询借还记录VO列表")
    @PostMapping("/query/serviceBorrowReturn")
    public R<Page<ServiceBorrowReturnVO>> queryServiceBorrowReturn(@RequestBody PageParams<ServiceBorrowReturnDTO> pageParams) {
        log.info("queryServiceBorrowReturn - 分页查询借还记录VO列表, pageParams={}", pageParams);
        try {
            Page<ServiceBorrowReturnVO> page = serviceBorrowReturnService.listServiceBorrowReturnVOs(pageParams);
            return R.success(page);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * ID获取借还记录.
     *
     * @param id the id
     * @return the by id
     */
    @ApiOperation("ID获取借还记录")
    @GetMapping("/get/{id}")
    public R<ServiceBorrowReturnVO> getById(@PathVariable("id") Long id) {
        log.info("getById - ID获取借还记录, id={}", id);
        try {
            return R.successDef(serviceBorrowReturnService.getServiceBorrowReturnVOById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 借用服务.
     *
     * @param serviceBorrowReturnDTO the service borrow return dto
     * @return the r
     */
    @ApiOperation("借用服务")
    @SysLog("借用服务")
    @PostMapping("/borrow")
    public R<Boolean> borrow(@RequestBody ServiceBorrowReturnDTO serviceBorrowReturnDTO) {
        log.info("borrow - 借用服务, serviceBorrowReturnDTO={}", serviceBorrowReturnDTO);
        try {
            return R.successDef(serviceBorrowReturnService.borrowService(serviceBorrowReturnDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 归还服务.
     *
     * @param serviceBorrowReturnDTO the service borrow return dto
     * @return the r
     */
    @ApiOperation("归还服务")
    @SysLog("归还服务")
    @PostMapping("/return")
    public R<Boolean> returnService(@RequestBody ServiceBorrowReturnDTO serviceBorrowReturnDTO) {
        log.info("returnService - 归还服务, serviceBorrowReturnDTO={}", serviceBorrowReturnDTO);
        try {
            return R.successDef(serviceBorrowReturnService.returnService(serviceBorrowReturnDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
